package tsing.javase.async.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tsing.javase.async.AsyncFactory;
import tsing.javase.async.AsyncManager;
import tsing.javase.common.bean.LoginBody;
import tsing.javase.common.enums.ResultCode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author Tsing/吴清平
 * @Date 2022/10/18 15:24
 * @Version 1.0
 */
@RestController
public class LoginControllerDemo {

    private static final Logger log = LoggerFactory.getLogger("asynclog");

    @RequestMapping("/async/login")
    public String login(@RequestBody LoginBody loginBody) {
        String result = "";
        if ("root".equals(loginBody.getUsername()) && "root".equals(loginBody.getPassword())) {
            result = "SUCCESS";
            ResultCode success = ResultCode.SUCCESS;
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(loginBody.getUsername(), success.code.toString(), success.message));
            for (int i = 0; i < 10; i++) {
                LocalDateTime now = LocalDateTime.now();
                log.info("成功" + now.format(DateTimeFormatter.ofPattern("yyyy-mm-dd HH:mm:ss")));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } else {
            result = "FAIL";
            ResultCode success = ResultCode.ERROR;
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(loginBody.getUsername(), success.code.toString(), success.message));
            for (int i = 0; i < 10; i++) {
                LocalDateTime now = LocalDateTime.now();
                log.info(result + now.format(DateTimeFormatter.ofPattern("yyyy-mm-dd HH:mm:ss")));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }
        return result;
    }
}
